package modele.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import modele.beans.Employee;
import modele.database.IDatabase;

public class EmployeeDAO {
	private IDatabase conn;
	
	public EmployeeDAO(IDatabase conn) {
		this.conn = conn; 
	}
	
	public ArrayList<Employee> GetEmployees() throws SQLException, ClassNotFoundException {
		conn.Connect();
		ArrayList<ArrayList<Object>> empList = conn.Select("SELECT LAST_NAME, FIRST_NAME, ADDRESS, PHONE, EMPLOYEE_ID FROM EMPLOYEE");
		
		ArrayList<Employee> emps = new ArrayList<Employee>();
		
		for (int i = 0; i != empList.size(); ++i) {
			emps.add(new Employee((String)empList.get(i).get(0),
					(String)empList.get(i).get(1),
					(String)empList.get(i).get(2),
					(String)empList.get(i).get(3),
					empList.get(i).get(4).toString()));
		}
		
		conn.Close();
		
		return emps;
	}
	
	public void AddEmployee(Employee employee) throws SQLException, ClassNotFoundException {
		conn.Connect();
		
		conn.Insert("INSERT INTO EMPLOYEE(LAST_NAME, FIRST_NAME, ADDRESS, PHONE, EMPLOYEE_ID) "
				+ " SELECT ?, ?, ?, ?, employee_seq.nextval FROM DUAL", 
				employee.getName(), employee.getFirstName(), employee.getAddress(), employee.getPhoneNumber());
		
		conn.Close();
	}
	
	public void UpdateEmployee(Employee employee) throws SQLException, ClassNotFoundException {
		conn.Connect();
		conn.Update("UPDATE EMPLOYEE " +
					" SET LAST_NAME = ?, " +
					" FIRST_NAME = ?, " +
					" ADDRESS = ?, " +
					" PHONE = ? " +
					" WHERE EMPLOYEE_ID = ?", employee.getName(), employee.getFirstName(), employee.getAddress(), employee.getPhoneNumber(), employee.getId());
		conn.Close();
	}
	
	public void RemoveEmployee(Employee employee) throws SQLException, ClassNotFoundException {
		conn.Connect();
		conn.Update("DELETE FROM EMPLOYEE " +
					" WHERE EMPLOYEE_ID = ? ", employee.getId());
		conn.Close();
	}
}
